<?php

namespace app\admin\model;

use think\facade\Db;

class Classhourmodel extends Commonmodel {
    protected $otherTable;
    protected $studentTable;
    protected $recordDetailTable;

    public function __construct(array $data = [])
    {
        $this->table = 'zhjs_courseware_record';
        $this->pk = 'id';
        parent::__construct($data);
        $this->otherTable = 'zhjs_courseware_student';
        $this->studentTable = 'zhjs_student';
        $this->recordDetailTable = 'zhjs_courseware_record_detail';
    }

    public function saveClassHourInfo($dataArr, $otherArr, $detailArr, $id){
        if(!$dataArr || !$otherArr)  return false;
        $newData = [];
        $dataArr['people_total'] = count($otherArr);//print_r($detailArr);exit;

        Db::startTrans();
        try {
            if($id){
                $dataArr['examine_status'] = 1;         //内容被修改后，重新进入审核状态待审
                Db::table($this->table)->where(['id'=>$id])->update($dataArr);
                Db::table($this->otherTable)->where(['cid'=>$id])->delete();
                Db::table($this->recordDetailTable)->where(['id'=>$id])->delete();
//                foreach($otherArr as $key=>$val){
//                    $newData[$key]['sid'] = $val;
//                    $newData[$key]['cid'] = $id;
//                }
//                foreach($detailArr as $key=>$val){
//                    $detailArr[$key]['id'] = $id;
//                }
//                Db::table($this->otherTable)->insertAll($newData);
//                Db::table($this->recordDetailTable)->insertAll($detailArr);
            }else {
                Db::table($this->table)->save($dataArr);
                $id = Db::table($this->table)->getLastInsID();
            }
            foreach($otherArr as $key=>$val){
                $newData[$key]['sid'] = $val;
                $newData[$key]['cid'] = $id;
            }
            foreach($detailArr as $key=>$val){
                $detailArr[$key]['id'] = $id;
            }
            Db::table($this->otherTable)->insertAll($newData);
            Db::table($this->recordDetailTable)->insertAll($detailArr);
            Db::commit();
            return $id;
        } catch (\Exception $e) {
            trace('(saveClassHourInfo)保存\修改失败，添加内容为：' . json_encode($dataArr) . '失败原因：' . $e->getMessage(), 'error');
            Db::rollback();
            return false;
        }
    }

    public function getStudentsList($cid){
        if(!$cid)  return false;

        $data = self::db()
            ->alias('cr')
            ->rightJoin([$this->otherTable=>'cs'], 'cr.id=cs.cid')
            ->leftJoin([$this->studentTable=>'s'], 'cs.sid=s.id and s.status=1 and s.is_delete=1')
            ->where(['cs.cid'=>$cid])
            ->field('s.id, s.username')
            ->select();

        $data = $data ? $data->toArray() : '';

        $data = count($data) ? $data : false;
        return $data;
    }

    public function getStudensIds($id){
        if(!$id)  return false;

        $data = self::db()
            ->alias('cr')
            ->leftJoin([$this->otherTable=>'cs'], 'cr.id=cs.cid')
            ->field('cs.sid')
            ->where(['cs.cid'=>$id])
            ->select();

        if($data){
            $res = [];
            $data = $data->toArray();
            foreach($data as $val)  $res[] = $val['sid'];
            return implode(',', $res);
        }

        return false;
    }

    public function findCourseDetailById($id){
        if(!$id)  return false;
        $listObj = Db::table($this->recordDetailTable)
            ->alias('d')
            ->leftJoin(['zhjs_content'=>'c'], 'c.id=d.cid')
            ->field('d.*, c.title')
            ->where(['d.id'=>$id])
            ->select();

        if(!$listObj)  return false;
        return $listObj->toArray();
    }

}

?>